package seGobangClient.onlineHall;

import java.util.*;
import seGobangClient.*;

/**
 *
 * @author plus
 */
public class OnlineHallController implements ISceneController{
    private static HashMap <String,ICommand> CommandsPool;
    static IOnlineHallScene Scene;                //可视场景
    public static int UID=0;
    public static String UserName;
    public static OnlineHallCommandsFactory Factory=new OnlineHallCommandsFactory();
    public static final long updateRate=1000; //(网络消息刷新频率)
    private Server MyServer;

    public OnlineHallController(IUISystem Scene) {
        this.Scene=(IOnlineHallScene) Scene;
        UID=0;
        UserName=new String();
        Scene.setCurrentController(this);
        this.MyServer = new Server(this,"localhost");
        blendCommands() ;
        MyServer.testServer();
    }
    
    public void returnToDesk(){
        MyServer.resumeServer();
    }
    
    public void pauseController(){
        MyServer.pasueServer();
    }
    
    public void blendCommands() {
        CommandsPool=new HashMap <String,ICommand>();
        /*由UI发起的事件*/
        CommandsPool.put("TryLogin",Factory.createTryLoginCommand(MyServer));
        CommandsPool.put("UpdateRoomList",Factory.createUpdateRoomListCommand(MyServer));
        CommandsPool.put("JoinRoom",Factory.createJoinRoomCommand(MyServer));
        CommandsPool.put("ExitRoom",Factory.createExitRoomCommand(MyServer));
        CommandsPool.put("JoinDesk",Factory.createJoinDeskCommand(MyServer));
        CommandsPool.put("ExitDesk",Factory.createExitDeskCommand(MyServer));
        CommandsPool.put("ExitHall",Factory.createExitHallCommand((IMessageBox)Scene));
        CommandsPool.put("HaltHall",Factory.createHaltHallCommand(this));
        CommandsPool.put("GetReady", Factory.createGetReadyCommand(MyServer));
        CommandsPool.put("CancelReady", Factory.createCancelReadyCommand(MyServer));
        
         /*由Server发起的事件*/
        CommandsPool.put("LoginView",Factory.createLoginViewCommand(Scene));
        CommandsPool.put("RoomListView",Factory.createRoomListViewCommand(Scene));
        CommandsPool.put("RefreshRoomList",Factory.createRefreshRoomListCommand(Scene));
        CommandsPool.put("ExitHall",Factory.createExitHallCommand((IMessageBox)Scene));
        CommandsPool.put("ServerNotAvailable",Factory.createServerNotAvailableCommand((IMessageBox)Scene));
        CommandsPool.put("LoginFailed",Factory.createLoginFailedCommand((IMessageBox)Scene));
        
        /*由Room发起的事件*/
        CommandsPool.put("RoomView",Factory.createRoomViewCommand(Scene));
        CommandsPool.put("RefreshDeskState",Factory.createRefreshDeskStateCommand(Scene));
        
        /*由Desk发起的事件*/
        CommandsPool.put("DeskView",Factory.createDeskViewCommand(Scene));
        CommandsPool.put("RefreshRemotePlayerInfo",Factory.createRefreshRemotePlayerInfoCommand(Scene));
        CommandsPool.put("StartGame",Factory.createStartGameCommand(this));
        
        CommandsPool.put("ExitGame", new seGobangClient.mainMenu.CommandExitGame());
    }

    public Object doEvent(String CommandName, Object Param) {
        try{
                CommandsPool.get(CommandName).execute(Param);
        }catch(Exception e){
            System.err.println(CommandName);
            e.printStackTrace();
        }
        return null;
    }
    
}
